package com.business.action.oa;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.business.bean.basic.EngineerBean;
import com.business.bean.oa.DispatchBean;
import com.business.bean.oa.DispatchUserApplyBean;
import com.business.bean.oa.DispatchUserBean;
import com.business.bean.oa.DispatchWorkBean;
import com.business.bean.oa.ReportDay;
import com.business.bean.oa.ReportDayDetail;
import com.business.bean.oa.WorkApplyTime;
import com.business.bean.oa.WorkApplyTimeDetail;
import com.business.bean.record.DispatchCarRecord;
import com.business.bean.record.MealOrderRecord;
import com.business.bean.system.UserBean;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.rose.jdbc.KeyGenerator;
import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.dao.BeanDAO;
import net.sf.rose.jdbc.query.BeanSQL;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.util.StringUtil;
import net.sf.rose.web.util.WebUtils;

/**
 * 调度
 * 
 * @author fengjian
 *
 */
@Controller
@RequestMapping("/oa/dispatch")
public class DispatchAction {

	/**
	 * 获取某天的调度数据
	 */
	@ResponseBody
	@RequestMapping("/getDayDispatch.do")
	public PageBean getDayDispatch(Service service, String day) {
		PageBean page = new PageBean();
		UserBean user = (UserBean) service.getUser();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		// 根据日期查找调度记录,如果没有则创建
		query.setEntityClass(DispatchBean.class);
		query.createSql("calendarDate", day);
		DispatchBean dispatch = dao.get();
		if (dispatch == null) {
			dispatch = new DispatchBean();
			dispatch.setCalendarDate(day);
			dispatch.setDispatchID(user.getUserID());
			dispatch.setDispatcherName(user.getUserName());
			dispatch.setStatus(1);
			query.createSaveSql(dispatch);
			dao.update();
		}
		// 获取请假人员
		query.setEntityClass(WorkApplyTimeDetail.class);
		WorkApplyTimeDetail wd = new WorkApplyTimeDetail();
		wd.setCalendarDate(day);
		query.createSql(wd);
		List<WorkApplyTimeDetail> wolist = dao.list();
		Map<String, WorkApplyTimeDetail> wotMap = new HashMap<>();
		for (WorkApplyTimeDetail bean : wolist) {
			wotMap.put(bean.getUserID(), bean);
		}
		// 如果是在调度中
		if (dispatch.getStatus() == 1) {
			// 获取最近的一条调度记录作为模板
			DispatchWorkBean templetDispatch = null;
			query.setSQL("select dispatchID from OA_DISPATCH where calendarDate=");
			query.addSql("(select max(calendarDate) as calendarDate from OA_DISPATCH)");
			DispatchBean db = dao.get();
			if (db != null) {
				templetDispatch = new DispatchWorkBean();
				templetDispatch.setDispatchID(db.getDispatchID());
			}
			// 获取已创建的所有工程申请调度记录
			query.setEntityClass(DispatchWorkBean.class);
			query.createSql("dispatchID", dispatch.getDispatchID());
			query.addOrderby("viewSequence");
			List<DispatchWorkBean> works = dao.list();
			// 获取所有的在建工程和项目
			query.setEntityClass(EngineerBean.class);
			query.createSql("status", 1);
			List<EngineerBean> engineers = dao.list();
			// 如果已创建的分配工程数量小于在建的工程数量
			if (works.size() < engineers.size()) {
				Map<String, DispatchWorkBean> workMap = new HashMap<>();
				for (DispatchWorkBean bean : works) {
					workMap.put(bean.getEngineerID(), bean);
				}
				// 将缺失的在建工程全部加上
				for (EngineerBean en : engineers) {
					if (workMap.get(en.getEngineerID()) == null) {
						DispatchWorkBean bean = new DispatchWorkBean();
						bean.setDispatchID(dispatch.getDispatchID());
						bean.setCalendarDate(day);
						bean.setEngineerID(en.getEngineerID());
						bean.setDispatchID(user.getUserID());
						bean.setDispatcherName(user.getUserName());
						bean.setStatus(2);
						query.createSaveSql(bean);
						dao.update();
						works.add(bean);
					}
				}
			}
			// 遍历所有的工程申请调度记录
			for (DispatchWorkBean bean : works) {
				// 加载所有的人员调度记录
				loadAllDispatchUsers(dao, query, bean, templetDispatch, wotMap);
			}
			dispatch.setWorkList(works);
		}
		// 如果是已调度
		else if (dispatch.getStatus() > 1) {
			// 获取已创建的所有工程申请调度记录
			query.setEntityClass(DispatchWorkBean.class);
			query.createSql("dispatchID", dispatch.getDispatchID());
			query.addOrderby("viewSequence");
			List<DispatchWorkBean> works = dao.list();
			// 遍历添加所有工程人员调度记录
			query.setEntityClass(DispatchUserBean.class);
			query.setTableName("OA_DISPATCH_USER_VIEW");
			query.addOrderby("jobID");
			for (DispatchWorkBean bean : works) {
				query.createSql("dispatchWorkID", bean.getDispatchID());
				query.addOrderby("jobID");
				bean.setDispatchList(dao.list());
			}
		}

		// 遍历所有的工程申请调度记录
		for (DispatchWorkBean bean : dispatch.getWorkList()) {
			// 获取工程的派车记录
			query.setEntityClass(MealOrderRecord.class);
			query.createSql("dispatchWorkID", bean.getDispatchWorkID());
			bean.setMealOrder(dao.get());
			// 获取工程的订餐记录
			query.setEntityClass(DispatchCarRecord.class);
			query.createSql("dispatchWorkID", bean.getDispatchWorkID());
			bean.setDispatchCar(dao.get());
		}
		// 查出所有在职的工程人员列表
		query.setEntityClass(DispatchUserBean.class);
		query.setTableName("OA_USER_DISPATCH_VIEW");
		query.createSql();
		query.addOrderby("jobID");
		List<DispatchUserBean> userlist = dao.list();
		Map<String, DispatchUserBean> map = new LinkedHashMap<>();
		for (DispatchUserBean u : userlist) {
			map.put(u.getUserID(), u);
		}
		// 删除已分配人员
		for (DispatchWorkBean bean : dispatch.getWorkList()) {
			for (DispatchUserBean du : bean.getDispatchList()) {
				if (du.getUserID() != null) {
					map.remove(du.getUserID());
				}
			}
		}
		// 标记请假人员
		userlist.clear();
		for (DispatchUserBean bean : map.values()) {
			WorkApplyTimeDetail wat = wotMap.get(bean.getUserID());
			bean.setApplyType(wat.getApplyType());
			userlist.add(bean);
		}
		page.setData(dispatch);
		return page;
	}

	/**
	 * 给工程申请调度类加载所有的人员调度记录
	 */
	private void loadAllDispatchUsers(BeanDAO dao, BeanSQL query, DispatchWorkBean bean,
			DispatchWorkBean templetDispatch, Map<String, WorkApplyTimeDetail> wotMap) {
		// 查找本工程已分配的人员
		query.setEntityClass(DispatchUserBean.class);
		query.createSql("dispatchWorkID", bean.getDispatchWorkID());
		List<DispatchUserBean> dus = dao.list();
		if (dus.size() > 0) {
			bean.setDispatchList(dus);
		}
		// 本工程没有分配的人员
		else {
			// 查找人员申请
			query.setEntityClass(DispatchUserApplyBean.class);
			query.createSql("dispatchWorkID", bean.getDispatchWorkID());
			List<DispatchUserApplyBean> applylist = dao.list();
			// 查找模板
			List<DispatchUserBean> modelist = new ArrayList<>();
			if (templetDispatch != null) {
				templetDispatch.setEngineerID(bean.getEngineerID());
				query.setEntityClass(DispatchWorkBean.class);
				query.createSql(templetDispatch);
				DispatchWorkBean dw = dao.get();
				if (dw != null) {
					query.setEntityClass(DispatchUserBean.class);
					query.createSql("dispatchWorkID", dw.getDispatchWorkID());
					modelist = dao.list();
					// 遍历模板
					int type = 0;
					List<DispatchUserBean> list = new ArrayList<>();
					for (DispatchUserBean du : modelist) {
						// 查找每个人员的请假记录
						WorkApplyTimeDetail wd = wotMap.get(du.getUserID());
						type = 0;
						// 如果有请假
						if (wd != null) {
							type = wd.getApplyType();
							// 如果不是请全天假
							if (type <= WorkApplyTime.APPLY_TYPE_AFTERNOON) {
								du.setApplyType(type);
							}
						}
						// 如果是请全天假
						if (type > WorkApplyTime.APPLY_TYPE_AFTERNOON) {
							// 则把人员信息清掉
							du.setUserID(null);
						}
						du.setDispatchUserID(null);
						list.add(du);
					}
					// 将有工号的数据排在前面
					modelist.clear();
					for (DispatchUserBean db : list) {
						if (db.getUserID() != null) {
							modelist.add(db);
						}
					}
					for (DispatchUserBean db : list) {
						if (db.getUserID() == null) {
							modelist.add(db);
						}
					}
				}
			}
			// 如果既有申请，又有模板
			if (applylist.size() > 0 && modelist.size() > 0) {
				// 把已有的模板缓存起来
				Map<String, List<DispatchUserBean>> map = new HashMap<>();
				for (DispatchUserBean du : modelist) {
					List<DispatchUserBean> list = map.get(du.getJobID());
					if (list == null) {
						list = new ArrayList<>();
					}
					list.add(du);
				}
				dus = new ArrayList<>();
				// 遍历所有的申请，如果某个工种申请的数量比模板多那就加上，如果少那就去掉
				for (DispatchUserApplyBean apply : applylist) {
					List<DispatchUserBean> list = map.get(apply.getJobID());
					// 如果没有申请
					if (list == null) {
						for (int i = 0; i < apply.getApplyCount(); i++) {
							DispatchUserBean du = new DispatchUserBean();
							du.setDispatchWorkID(apply.getDispatchWorkID());
							du.setJobID(apply.getJobID());
							du.setEngineerID(bean.getEngineerID());
							dus.add(du);
						}
					}
					// 如果申请的数量大于模板中人员的数量
					else if (apply.getApplyCount() > list.size()) {
						// 将模板先加上
						for (DispatchUserBean du : list) {
							du.setDispatchUserID(null);
							dus.add(du);
						}
						// 再追加
						for (int i = 0; i < apply.getApplyCount() - list.size(); i++) {
							DispatchUserBean du = new DispatchUserBean();
							du.setDispatchWorkID(apply.getDispatchWorkID());
							du.setJobID(apply.getJobID());
							du.setEngineerID(bean.getEngineerID());
							dus.add(du);
						}
					} else if (apply.getApplyCount() < list.size()) {
						// 只取模板的一部分
						for (int i = 0; i < apply.getApplyCount(); i++) {
							DispatchUserBean du = list.get(i);
							du.setDispatchUserID(null);
							dus.add(du);
						}
					}
				}
			}
			// 只有申请
			else if (applylist.size() > 0) {
				// 新生成人员调度记录
				dus = new ArrayList<>();
				for (DispatchUserApplyBean apply : applylist) {
					for (int i = 0; i < apply.getApplyCount(); i++) {
						DispatchUserBean du = new DispatchUserBean();
						du.setDispatchWorkID(apply.getDispatchWorkID());
						du.setJobID(apply.getJobID());
						du.setEngineerID(bean.getEngineerID());
						dus.add(du);
					}
				}
			}
			// 只有模板
			else if (modelist.size() > 0) {
				// 直接用模板生成人员调度记录
				dus = modelist;
			}
			if (dus.size() > 0) {
				query.createBatchInsertSql(dus);
				dao.batchUpdate();
			}
		}
	}

	/**
	 * 保存调度记录
	 */
	@ResponseBody
	@RequestMapping("/dispatch/save.do")
	public PageBean dispatchSave(Service service, DispatchBean bean) {
		PageBean page = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(DispatchBean.class);
		query.createUpdateSql(bean);
		dao.update();
		return page;
	}

	/**
	 * 获取工程调度数据列表
	 */
	@ResponseBody
	@RequestMapping("/work/list.do")
	public PageBean listDispatchWork(HttpServletRequest request, Service service) {
		PageBean page = new PageBean();
		Map<String, Object> map = WebUtils.getRequestData(request);
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(DispatchWorkBean.class);
		query.setPage(page);
		query.createSql(map);
		dao.list();
		return page;
	}

	/**
	 * 获取某条工程调度人员记录
	 */
	@ResponseBody
	@RequestMapping("/work/get.do")
	public PageBean getDispatchWork(Service service, String dispatchWorkID) {
		PageBean page = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(DispatchUserBean.class);
		query.setTableName("OA_DISPATCH_USER_VIEW");
		query.createSql("dispatchWorkID", dispatchWorkID);
		query.addOrderby("engineerID");
		List<DispatchUserBean> list = dao.list();
		List<DispatchUserBean> nolist = new ArrayList<>();
		Map<String, List<DispatchUserBean>> map = new LinkedHashMap<>();
		for (DispatchUserBean bean : list) {
			if (bean.getProjectID() == null) {
				nolist.add(bean);
			} else {
				List<DispatchUserBean> l = map.get(bean.getProjectID());
				if (l == null) {
					l = new ArrayList<>();
					map.put(bean.getProjectID(), l);
				}
				l.add(bean);
			}
		}
		List<List<DispatchUserBean>> plist = new ArrayList<>();
		for (List<DispatchUserBean> l : map.values()) {
			plist.add(l);
		}
		page.addDataMap("nolist", nolist);
		page.addDataMap("list", plist);
		return page;
	}

	/**
	 * 保存调度人员记录
	 */
	@ResponseBody
	@RequestMapping("/dispatchUser/save.do")
	public PageBean saveDispatchUser(Service service, String json) {
		PageBean page = new PageBean();
		List<DispatchUserBean> list = StringUtil.parseJsonArray(DispatchUserBean.class, json);
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(DispatchUserBean.class);
		for (DispatchUserBean bean : list) {
			query.createSaveSql(bean);
			dao.update();
		}
		// 查找未调度的工程记录，如果没有就调度完成
		String dispatchWorkID = list.get(0).getDispatchWorkID();
		query.setEntityClass(DispatchWorkBean.class);
		query.setSQL("select dispatchID from OA_DISPATCH_WORK where status=1 and dispatchWorkID =?");
		query.addParameter(dispatchWorkID);
		List<DispatchWorkBean> dwlist = dao.list();
		if (dwlist.size() == 0) {
			// 更新工程
			query.setSQL("update OA_DISPATCH_WORK set status=? where dispatchWorkID=?");
			query.addParameter(DispatchBean.TYPE_DISPATCH_OK);
			query.addParameter(dispatchWorkID);
			dao.update();
			// 更新调度
			query.setSQL("select dispatchID from OA_DISPATCH_WORK where dispatchWorkID=?");
			query.addParameter(dispatchWorkID);
			DispatchWorkBean dw = dao.get();
			query.setSQL("update OA_DISPATCH set status=? where dispatchID=?");
			query.addParameter(DispatchBean.TYPE_DISPATCH_OK);
			query.addParameter(dw.getDispatchID());
			dao.update();
		}
		return page;
	}

	/**
	 * 保存调度人员分配项目记录
	 */
	@ResponseBody
	@RequestMapping("/dispatchUser/saveProject.do")
	public PageBean saveDispatchUserProject(Service service, String json) {
		PageBean page = new PageBean();
		List<DispatchUserBean> list = StringUtil.parseJsonArray(DispatchUserBean.class, json);
		String sql = "update OA_DISPATCH_USER set projectID=#projectID# where dispatchUserID=#dispatchUserID#";
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(DispatchUserBean.class);
		query.createBatchSql(sql, list);
		dao.batchUpdate();
		// 查找未调试的工程记录，如果没有就调度完成
		query.setEntityClass(DispatchWorkBean.class);
		String dispatchWorkID = list.get(0).getDispatchWorkID();
		query.setSQL("select dispatchID from OA_DISPATCH_WORK where status=1 and dispatchWorkID =?");
		query.addParameter(list.get(0).getDispatchWorkID());
		List<DispatchWorkBean> dwlist = dao.list();
		if (dwlist.size() == 0) {
			// 更新工程
			query.setSQL("update OA_DISPATCH_WORK set status=? where dispatchWorkID=?");
			query.addParameter(DispatchBean.TYPE_DISTRIBUTE_OK);
			query.addParameter(dispatchWorkID);
			dao.update();
			// 更新调度
			query.setSQL("select dispatchID from OA_DISPATCH_WORK where dispatchWorkID=?");
			query.addParameter(dispatchWorkID);
			DispatchWorkBean dw = dao.get();
			query.setSQL("update OA_DISPATCH set status=? where dispatchID=?");
			query.addParameter(DispatchBean.TYPE_DISTRIBUTE_OK);
			query.addParameter(dw.getDispatchID());
			dao.update();
		}
		return page;
	}

	/**
	 * 获取某条工程调度下，具体某个项目的所有人员日报明细记录 用调度编号和项目编号查询
	 */
	@ResponseBody
	@RequestMapping("/work/getConfirmUserList.do")
	public PageBean getConfirmProjectUserList(Service service, String dispatchWorkID, String projectID) {
		PageBean page = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(ReportDayDetail.class);
		ReportDayDetail detail = new ReportDayDetail();
		detail.setDispatchWorkID(dispatchWorkID);
		detail.setProjectID(projectID);
		query.createSql(detail);
		List<ReportDayDetail> list = dao.list();
		if (list.size() > 0) {
			page.setData(list);
			return page;
		}
		// 如果没有，就用工程人员调度记录生成
		else {
			DispatchUserBean bean = new DispatchUserBean();
			bean.setDispatchWorkID(dispatchWorkID);
			bean.setProjectID(projectID);
			query.createSql(bean);
			List<DispatchUserBean> ul = dao.list();
			query.setEntityClass(DispatchWorkBean.class);
			query.createSql(dispatchWorkID);
			DispatchWorkBean work = dao.get();
			String calendar = work.getCalendarDate();
			for (DispatchUserBean du : ul) {
				ReportDayDetail db = new ReportDayDetail();
				db.setCalendarDate(calendar);
				db.setUserID(du.getUserID());
				db.setDispatchWorkID(dispatchWorkID);
				db.setProjectID(projectID);
				// 设置上下班时间
				list.add(db);
			}
			query.createBatchInsertSql(list);
			dao.batchUpdate();
			service.commit();
			// 重新获取
			query.setEntityClass(ReportDayDetail.class);
			query.createSql(detail);
			page.setData(dao.list());
		}
		return page;
	}

	/**
	 * 确认项目工人工时，以工程调度和项目为单位
	 */
	@ResponseBody
	@RequestMapping("/work/comfirmWorktime.do")
	public PageBean comfirmWorktime(Service service, String json) {
		PageBean page = new PageBean();
		UserBean user = (UserBean) service.getUser();
		List<ReportDayDetail> list = StringUtil.parseJsonArray(ReportDayDetail.class, json);
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		// 更新日报明细中的工时
		query.setEntityClass(ReportDayDetail.class);
		query.createBatchUpdateSql("workTime,overTime", list);
		dao.batchUpdate();
		service.commit();
		// 重新计算日报
		// 重新取出所有日报明细
		ReportDayDetail rd = list.get(0);
		ReportDayDetail r = new ReportDayDetail();
		r.setDispatchWorkID(rd.getDispatchWorkID());
		r.setProjectID(rd.getProjectID());
		query.createSql(r);
		list = dao.list();
		// 取出所有日报
		query.setEntityClass(ReportDay.class);
		ReportDay rp = new ReportDay();
		rp.setDispatchWorkID(rd.getDispatchWorkID());
		rp.setProjectID(rd.getProjectID());
		query.createSql(rp);
		List<ReportDay> rplist = dao.list();
		Map<String, ReportDay> map = new HashMap<>();
		for (ReportDay d : rplist) {
			map.put(d.getUserID(), d);
			// 将工时数据清空
			d.setWorkTime(0);
			d.setTotalWorkTime(0);
			d.setOverTime(0);
			d.setOverTimeDay(0);
			d.setOverTimeHoliday(0);
			d.setOverTimeWeekend(0);
		}
		// 获取工程申请调度表
		query.setEntityClass(DispatchWorkBean.class);
		query.createSql(rd.getDispatchWorkID());
		DispatchWorkBean work = dao.get();
		Date today = new Date();
		// 遍历所有日报明细，合成日报
		List<ReportDay> inlist = new ArrayList<>();
		List<ReportDay> uplist = new ArrayList<>();
		for (ReportDayDetail d : list) {
			ReportDay bean = map.get(d.getUserID());
			if (bean == null) {
				// 如果根据日报明细没有找到日报，就创建日报
				bean = new ReportDay();
				map.put(d.getUserID(), bean);
				bean.setDayReportID(KeyGenerator.getSystemUUID());
				bean.setDispatchID(work.getDispatchID());
				bean.setDispatchWorkID(work.getDispatchWorkID());
				bean.setUserID(d.getUserID());
				bean.setEngineerID(work.getEngineerID());
				bean.setProjectID(d.getProjectID());
				bean.setCalendarDate(work.getCalendarDate());
				inlist.add(bean);
			} else {
				uplist.add(bean);
			}
			bean.setConfirmUserID(user.getUserID());
			bean.setWorkTime(d.getOverTime() + bean.getWorkTime());
			bean.setOverTime(d.getOverTime() + bean.getOverTime());
			bean.setTotalWorkTime(bean.getWorkTime() + bean.getOverTime());
			bean.setConfirmTime(today);
			// 更新日报明细中对应的日报主键
			d.setDayReportID(bean.getDayReportID());
			d.setConfirmUserID(user.getUserID());
		}
		// 保存日报记录
		if (inlist.size() > 0) {
			query.createBatchInsertSql(inlist);
			dao.batchUpdate();
		}
		if (uplist.size() > 0) {
			query.createBatchUpdateSql("confirmUserID,workTime,overTime,totalWorkTime", uplist);
			dao.batchUpdate();
		}
		// 更新日报明细与日报关联信息
		query.createBatchUpdateSql("confirmUserID,dayReportID", list);
		dao.batchUpdate();
		service.commit();
		return page;
	}

	/**
	 * 校对项目工人工时
	 */
	@ResponseBody
	@RequestMapping("/work/checkWorktime.do")
	public PageBean checkWorktime(Service service, String json) {
		PageBean page = new PageBean();
		UserBean user = (UserBean) service.getUser();
		List<ReportDayDetail> list = StringUtil.parseJsonArray(ReportDayDetail.class, json);
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		// 更新日报明细中的工时
		query.setEntityClass(ReportDayDetail.class);
		query.createBatchUpdateSql("workTime,overTime", list);
		dao.batchUpdate();
		service.commit();
		// 重新计算日报
		// 重新取出所有日报明细
		ReportDayDetail rd = list.get(0);
		ReportDayDetail r = new ReportDayDetail();
		r.setDispatchWorkID(rd.getDispatchWorkID());
		r.setProjectID(rd.getProjectID());
		query.createSql(r);
		list = dao.list();
		// 取出所有日报
		query.setEntityClass(ReportDay.class);
		ReportDay rp = new ReportDay();
		rp.setDispatchWorkID(rd.getDispatchWorkID());
		rp.setProjectID(rd.getProjectID());
		query.createSql(rp);
		List<ReportDay> rplist = dao.list();
		Map<String, ReportDay> map = new HashMap<>();
		for (ReportDay d : rplist) {
			map.put(d.getUserID(), d);
			// 将工时数据清空
			d.setWorkTime(0);
			d.setTotalWorkTime(0);
			d.setOverTime(0);
			d.setOverTimeDay(0);
			d.setOverTimeHoliday(0);
			d.setOverTimeWeekend(0);
		}
		// 遍历所有日报明细，合成日报
		Date today = new Date();
		List<ReportDay> uplist = new ArrayList<>();
		for (ReportDayDetail d : list) {
			ReportDay bean = map.get(d.getUserID());
			uplist.add(bean);
			bean.setConfirmUserID(user.getUserID());
			bean.setWorkTime(d.getOverTime() + bean.getWorkTime());
			bean.setOverTime(d.getOverTime() + bean.getOverTime());
			bean.setTotalWorkTime(bean.getWorkTime() + bean.getOverTime());
			bean.setCheckedTime(today);
			// 更新日报明细
			d.setCheckUserID(user.getUserID());
		}
		if (uplist.size() > 0) {
			query.createBatchUpdateSql("checkUserID,workTime,overTime,totalWorkTime", uplist);
			dao.batchUpdate();
		}
		service.commit();
		return page;
	}

	/**
	 * 审核项目工人工时
	 */
	@ResponseBody
	@RequestMapping("/work/examineWorktime.do")
	public PageBean examineWorktime(Service service, String json) {
		PageBean page = new PageBean();
		UserBean user = (UserBean) service.getUser();
		List<ReportDayDetail> list = StringUtil.parseJsonArray(ReportDayDetail.class, json);
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		// 更新日报明细中的工时
		query.setEntityClass(ReportDayDetail.class);
		query.createBatchUpdateSql("workTime,overTime", list);
		dao.batchUpdate();
		service.commit();
		// 重新计算日报
		// 重新取出所有日报明细
		ReportDayDetail rd = list.get(0);
		ReportDayDetail r = new ReportDayDetail();
		r.setDispatchWorkID(rd.getDispatchWorkID());
		r.setProjectID(rd.getProjectID());
		query.createSql(r);
		list = dao.list();
		// 取出所有日报
		query.setEntityClass(ReportDay.class);
		ReportDay rp = new ReportDay();
		rp.setDispatchWorkID(rd.getDispatchWorkID());
		rp.setProjectID(rd.getProjectID());
		query.createSql(rp);
		List<ReportDay> rplist = dao.list();
		Map<String, ReportDay> map = new HashMap<>();
		for (ReportDay d : rplist) {
			map.put(d.getUserID(), d);
			// 将工时数据清空
			d.setWorkTime(0);
			d.setTotalWorkTime(0);
			d.setOverTime(0);
			d.setOverTimeDay(0);
			d.setOverTimeHoliday(0);
			d.setOverTimeWeekend(0);
		}
		// 遍历所有日报明细，合成日报
		Date today = new Date();
		List<ReportDay> uplist = new ArrayList<>();
		for (ReportDayDetail d : list) {
			ReportDay bean = map.get(d.getUserID());
			uplist.add(bean);
			bean.setConfirmUserID(user.getUserID());
			bean.setWorkTime(d.getOverTime() + bean.getWorkTime());
			bean.setOverTime(d.getOverTime() + bean.getOverTime());
			bean.setTotalWorkTime(bean.getWorkTime() + bean.getOverTime());
			bean.setExamineTime(today);
			// 更新日报明细
			d.setExamineUserID(user.getUserID());
		}
		if (uplist.size() > 0) {
			query.createBatchUpdateSql("examineUserID,workTime,overTime,totalWorkTime", uplist);
			dao.batchUpdate();
		}
		service.commit();
		return page;
	}
}
